﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Mulberry.Model.Ent;
using OMG.Fill.Mulberry.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Mulberry.Model.UI.Controllers
{
    public class MulAnnualRearController : ApiController
    {
        MulAnnualRearServices mulAnnualRearServices = new MulAnnualRearServices();
        ReportServices reportServices = new ReportServices();
        DictServices dict = new DictServices();

        //[ApiActionAuthorize("获取表4全年饲养蚕品种构成调查表T_MulAnnualRear分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<MulAnnualRearEntity>> PostPageList(MulAnnualRearQuery query)
        {
            return mulAnnualRearServices.GetPageList(query);
        }
        public ResResult<List<MulAnnualRearStatsModel>> PostStatsList(MulNowModel model)
        {
            return mulAnnualRearServices.GetStatsList(model);
        }
        public ResResult<List<MulAnnualRearStatsModel>> PostStatsDetailList(MulNowModel model)
        {
            return mulAnnualRearServices.GetDetailStats(model);
        }
        public ResResult<List<MulAnnualRearStatsModel>> PostStatsTimeList(MulNowModel model)
        {
            return mulAnnualRearServices.GetStatsByTimeList(model);
        }
        //[ApiActionAuthorize("新增表4全年饲养蚕品种构成调查表T_MulAnnualRear指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(List<MulAnnualRearEntity> model) {
            return mulAnnualRearServices.AddInfo(model);
        }
        //public ResResult<List<MulAreaStatists>> PostStatsList(MulNowModel model)
        //{
        //    mulAnnualRearServices.
        //}
        //[ApiActionAuthorize("修改表4全年饲养蚕品种构成调查表T_MulAnnualRear指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(MulAnnualRearEntity model)
        {
            return mulAnnualRearServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除表4全年饲养蚕品种构成调查表T_MulAnnualRear指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return mulAnnualRearServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取表4全年饲养蚕品种构成调查表T_MulAnnualRear指定对象操作")]
        //[ApiActionException]
        public ResResult<MulAnnualRearEntity> Get(string id)
        {
            return mulAnnualRearServices.GetModel(id);
        }

        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportCode, string FillInYear, string AreaCode)
        {
            MulNowModel model = new MulNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.ReportCode = ReportCode;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear + "全年饲养蚕品种构成调查表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulAnnualRearServices.Method(model).GetBuffer());
            curContext.Response.End();
        }

        public void GetStatsExcelData(string FillInYear, string AreaCode, string Status,string itemText)
        {
            
            MulNowModel model = new MulNowModel();
            model.FillInYear = FillInYear;
            model.AreaCode = AreaCode;
            model.Status = Status;
            model.itemText = itemText;//饲养蚕品种
            string itemTextName = "";
            if (itemText != null && itemText != "")
            {
                itemTextName = dict.GetPZName(itemText, "CSCultivar");
            }
            else
            {
                itemTextName = "全部";
            }
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = FillInYear  + "饲养蚕品种构成调查表("+itemTextName+").xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulAnnualRearServices.MethodStats(model,itemTextName).GetBuffer());
            curContext.Response.End();
        }
    }
}